<template>
  <el-main class="main-container" style="background-color: f3f2f4;">
    <!-- 页头 -->
    <el-row>
      <el-col :span="24">
        <el-form :inline="true" style="float:left">
          <!-- 关键词 -->
          <el-form-item label="游戏名称" style="margin-left:10px;" class="igf-margin-bottom">
            <el-input style="width:150px" size="small" v-model="requestData.postData.keyword"></el-input>
          </el-form-item>
          <el-form-item style="margin-left:10px;" class="igf-margin-bottom">
            <el-button size="small" @click="search" round>立即查询</el-button>
          </el-form-item>
          <el-form-item style="margin-left:10px;" class="igf-margin-bottom">
            <span style="color:#868686">热门搜索：</span>
            <span
              v-for="item in hotSearch"
              class="search-text"
              @click="clickSearchText(item)"
            >{{item}}</span>
          </el-form-item>
        </el-form>
        <el-form :inline="true" style="float:right">
          <el-form-item class="igf-margin-bottom">
            <span style="color:#868686">筛选结果：</span>
            <span>全部</span>
            <span style="color:#007ec0">{{total}}</span>
            <span>款游戏</span>
          </el-form-item>
          <el-form-item class="igf-margin-bottom">
            <el-button-group>
              <div class="refresh-btn" @click="init">
                <i class="el-icon-refresh" style="font-size:25px;"></i>
              </div>
              <el-button
                size="small"
                icon="el-icon-arrow-left"
                @click="prev"
                :disabled="requestData.postData.page_num == 1"
              ></el-button>
              <el-button
                size="small"
                :disabled="requestData.postData.page_num > total/requestData.postData.page_size"
                @click="next"
              >
                <i class="el-icon-arrow-right el-icon--right"></i>
              </el-button>
            </el-button-group>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <div style="margin:0 10px;">
      <div class="igf-divider"></div>
    </div>

    <!-- 内容 -->
    <el-container>
      <games-widget :list="tableData" />
    </el-container>
    <!-- 页尾 -->
    <el-row style="padding-top:10px;">
      <el-col :span="24">
        <el-pagination
          style="float:right"
          background
          layout="total,prev, pager, next,jumper"
          :total="total"
          :page-size="requestData.postData.page_size?requestData.postData.page_size:20"
          :current-page="requestData.postData.page_num"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </el-col>
    </el-row>
  </el-main>
</template>
<script>
import Table from "@/components/Table.vue";
import gamesWidget from "./components/gamesWidget";
export default {
  components: {
    Table,
    gamesWidget
  },
  data() {
    return {
      requestData: {
        urlPath: "/game/list",
        postData: {
          keyword: "",
          page_num: 1,
          page_size: 20
        }
      },
      total: 0,
      tableData: [],
      hotSearch: [
        "文明6",
        "三国志13",
        "如龙",
        "古墓丽影",
        "只狼",
        "GTA5",
        "生化危机7"
      ]
    };
  },
  methods: {
    search() {
      this.init();
    },
    handleCurrentChange(val) {
      this.requestData.postData.page_num = val;
      this.init();
    },
    prev: function() {
      this.requestData.postData.page_num--;
      if (this.requestData.postData.page_num <= 0) {
        this.requestData.postData.page_num = 1;
      }
      this.init();
    },
    next: function() {
      this.requestData.postData.page_num++;
      this.init();
    },
    init() {
      this.$http
        .post(this.requestData.urlPath, this.requestData.postData)
        .then(res => {
          if (res.code === 200) {
            this.total = res.data.total;
            this.tableData = res.data.list;
          } else {
            this.$message.error("数据获取失败!");
          }
        })
        .catch(err => {
          this.$message.error(err.msg);
        });
    },
    clickSearchText(text) {
      this.requestData.postData.keyword = text;
      this.search();
    }
  },
  created() {
    this.init();
  }
};
</script>
<style scoped>
.igf-margin-bottom {
  margin-bottom: 0;
}
.igf-divider {
  display: block;
  height: 1px;
  width: 100%;
  margin: 15px 0 10px 0;
  background-color: #dcdfe6;
  position: relative;
}
.refresh-btn {
  margin-right: 10px;
  float: left;
  cursor: pointer;
  color: #32cd32;
}
.refresh-btn:hover {
  color: #098109;
}
.refresh-btn:active {
  color: #32cd32;
}
.search-text {
  margin-right: 5px;
  cursor: pointer;
}
</style>